package com.zjt.util;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * 实体类和Model之间转换类
 */

public class ModelUtil {

    private static final Logger log = LoggerFactory.getLogger(ModelUtil.class);
	
	public static <A, B> B modelA2B(A modelA, Class<B> bClass) {
	    try {
	        Gson gson = new Gson();
	        String gsonA = gson.toJson(modelA);
	        B instanceB = gson.fromJson(gsonA, bClass);
	        return instanceB;
	    } catch (Exception e) {
	        return null;
	    }
	}
	
	 /**
     * 将entityList转换成modelList
     * @param fromList
     * @param tClass
     * @param <F>
     * @param <T>
     * @return
     */
    public static<F,T> List<T> entityListToModelList(List<F> fromList, Class<T> tClass){
        if(fromList.isEmpty() || fromList == null){
            return null;
        }
        List<T> tList = new ArrayList<>();
        for(F f : fromList){
            T t = entityToModel(f, tClass);
            tList.add(t);
        }
        return tList;
    }
 
    public static<F,T> T entityToModel(F entity, Class<T> modelClass) {
        log.debug("entityToModel : Entity属性的值赋值到Model");
        Object model = null;
        if (entity == null || modelClass ==null) {
            return null;
        }
 
        try {
            model = modelClass.newInstance();
        } catch (InstantiationException e) {
            log.error("entityToModel : 实例化异常", e);
        } catch (IllegalAccessException e) {
            log.error("entityToModel : 安全权限异常", e);
        }
        BeanUtils.copyProperties(entity, model);
        return (T)model;
    }

    /**
     *方法三
     * @param list
     * @return
     */
    public static List<TreeMenuNode> toTree(List<TreeMenuNode> list,Integer parentId) {
        List<TreeMenuNode> treeList = new ArrayList<TreeMenuNode>();
        for (TreeMenuNode tree : list) {
            if(tree.getpId().equals(parentId)){
                treeList.add(tree);
            }
        }
        for (TreeMenuNode treeMenuNode : list) {
            toTreeChildren(treeList,treeMenuNode);
        }
        return treeList;
    }
    
    public static JsonArray toJsonArray(List<TreeMenuNode> list) {
    	JsonArray jsonArray = new JsonArray();
    	if(!CollectionUtils.isEmpty(list)) {
    		for (TreeMenuNode treeMenuNode : list) {
    			JsonObject jsonObject = new JsonObject();
    			jsonObject.addProperty("id", treeMenuNode.getId()); // 节点id
	       		jsonObject.addProperty("pId", treeMenuNode.getpId()); // 节点id
	       		jsonObject.addProperty("name", treeMenuNode.getName()); // 节点id
	       		jsonObject.addProperty("state", treeMenuNode.getState()); // 节点id
	       		jsonObject.addProperty("iconValue", treeMenuNode.getIcon()); // 节点id
	       		if(CollectionUtils.isEmpty(treeMenuNode.getChildren())) {
	       			jsonObject.addProperty("open", "false"); // 节点id
	       		}else{
	       		    jsonObject.addProperty("open", "true"); // 有子节点
	       		    jsonObject.add("children", toJsonArray(treeMenuNode.getChildren()));
	       		}
	       		jsonArray.add(jsonObject);
    		}
    	}
        return jsonArray;
    }
    
    private static void toTreeChildren(List<TreeMenuNode> treeList, TreeMenuNode treeMenuNode) {
        for (TreeMenuNode node : treeList) {
            if(treeMenuNode.getpId().equals(node.getId())){
                if(node.getChildren() == null){
                    node.setChildren(new ArrayList<TreeMenuNode>());
                }
                node.getChildren().add(treeMenuNode);
            }
            if(node.getChildren() != null){
                toTreeChildren(node.getChildren(),treeMenuNode);
            }
        }
    }
    
    
}
